#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/11/15 下午7:19
# @Author  : yinxin
# @File    : detail_application
# @Software: PyCharm


import inspect
import time

import sppm

from common import delay, spider_config
from log import hlog
from modules import PLATFORM_SPIDER_MAP
from utils import Consumer, get_platform


def start_detail_crawl():
    func_name = inspect.stack()[0][3]
    hlog.enter_func(func_name)

    consumer = Consumer.get_consumer()
    while True:
        if sppm.signal_monitor():
            hlog.info('任务回调函数返回True，退出......')
            break

        msg = consumer.get_msg()

        # 锁定工作状态
        sppm.working_lock(True)

        url = bytes.decode(msg.value)
        platform = get_platform(url)
        if platform not in PLATFORM_SPIDER_MAP:
            hlog.error("获取到未知网站：%s" % platform)
            continue
        my_spider = PLATFORM_SPIDER_MAP[platform](url,
                                                  1,
                                                  spider_config.spider[platform])
        my_spider.detail_handle()

        # 解锁工作状态
        sppm.working_lock(False)

        hlog.debug("已完成url: %s, 准备休眠%ss后继续" % (url, delay))
        time.sleep(delay)


if __name__ == '__main__':
    # start_detail_crawl()

    sppm.sppm_start(start_detail_crawl)
